package com.gxa.clientkill.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.clientkill.pojo.ShowTicket;
import com.gxa.clientkill.service.ShowTicketService;
import com.gxa.clientkill.utils.JsonResult;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jun
 * @version 1.0
 * @date 2020/11/11 10:54
 */

@Api(value = "查看优惠券的控制器",tags = "查看优惠券接口的api")
@RestController
@RequestMapping("/sticket")
public class ShowTicketController {

    @Resource
    ShowTicketService showTicketService;
    JsonResult jsonResult = new JsonResult();

    @PostMapping("/list")
    @ApiOperation(value = "查看优惠券接口", notes = "用于优惠券使用记录详情的列表展示", httpMethod = "POST", response = Map.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "分页大小", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map list(@RequestParam("limit") int limit, @RequestParam("page") int page) {
//定义一个map
        Map<String, Object> stktMap = new HashMap<>();
        //将分页条件输入PageHelper
        Page<Object> page1 = PageHelper.startPage(page, limit);
        //将所有的优惠券信息查询出来
        List<ShowTicket> showTickets = showTicketService.list();
        //将分页信息与所有的优惠券信息结合
        PageInfo<ShowTicket> info = new PageInfo<>(showTickets);
        //将分页的列表导入map中
        stktMap.put("list",info.getList());
        //将总记录数导入map中
        stktMap.put("counts",info.getTotal());
        jsonResult.put("counts",stktMap.get("counts"));
        jsonResult.put("list",stktMap.get("list"));
        jsonResult.setCode("200");
        jsonResult.setMsg("分页查询成功");
        return jsonResult.getValues();
    }

    @PostMapping("/del")
    @ApiOperation(value = "优惠券使用记录删除", notes = "该接口用于删除优惠券使用记录", httpMethod = "POST", response = Map.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "优惠券id", required = true, dataType = "int", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map del(@RequestParam("id") int id) {
        Integer del = showTicketService.del(id);
        jsonResult.setData(del);
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getValues();
    }

}
